<template>
  <div>
    <div v-if="!select" style="display: flex;justify-content: space-between;align-items: center">
      <el-button type="primary" @click="addUser" icon="el-icon-circle-plus-outline">新增</el-button>
      <el-tag type="success" size="default">{{ branchInfo.groupAllName || '未知组织'}}</el-tag>
    </div>

    <el-table v-loading="loading" :data="dataSource">
      <el-table-column type="expand" align="center" label="详情">
        <template slot-scope="scope">
          <user-info :user-id="scope.row.userId" :edit-abled="false" />
        </template>
      </el-table-column>
      <el-table-column prop="nickName" label="名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.adminFlag">{{scope.row.nickName}}</el-tag>
          <span v-else>{{scope.row.nickName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="mobilePhone" label="手机号" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="userCode" label="编码" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="gender" label="性别" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="dataState" label="状态" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState >=0 ">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="130px">
        <div slot-scope="scope">
          <template v-if="select">
            <el-button type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
          </template>
          <template v-else>
            <el-button type="success" icon="el-icon-edit" circle @click="() => { entityInfo=$Util.copy(scope.row);visible=true}" title="编辑"></el-button>
            <el-button type="warning" icon=" iconfont icon-org-one" circle @click="openBranch(scope.row)" title="切换"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
          </template>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="用户信息">
      <el-form label-position="right" label-width="80px">
        <el-form-item label="名称" required>
          <el-input v-model="entityInfo.nickName" placeholder="用户名称"/>
        </el-form-item>
        <el-form-item label="手机号" required>
          <el-input v-model="entityInfo.mobilePhone" placeholder="手机号"/>
        </el-form-item>
        <el-form-item label="email" required>
          <el-input v-model="entityInfo.email" placeholder="邮箱" />
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="entityInfo.identityCard" placeholder="身份证号"/>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker v-model="entityInfo.birthDay" type="date" value-format="yyyy-MM-dd" placeholder="出生日期" style="width: 100%" />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="性别">
              <el-radio-group v-model="entityInfo.gender">
                <el-radio label="男">男</el-radio>
                <el-radio label="女">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态">
              <el-radio-group v-model="entityInfo.dataState">
                <el-radio :label="-1">禁用</el-radio>
                <el-radio :label="0">正常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8"></el-col>
        </el-row>
        <template v-if="branchInfo.branchId">
          <el-form-item  label="用户编码">
            <el-input v-model="entityInfo.userCode" placeholder="用户编码"/>
          </el-form-item>
          <el-form-item  label="管理员">
            <el-switch v-model="entityInfo.adminFlag"/>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(entityInfo)">{{ entityInfo.userGroupId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="branchVisible" title="部门切换">
      <el-form label-position="right" label-width="80px">
        <el-form-item label="管理员" required>
          <el-switch v-model="selectUser.adminFlag" />
        </el-form-item>
        <el-form-item label-width="0">
          <branch-table @rowSelect="switchBranch"></branch-table>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import UserInfo from "@/views/system/user/UserInfo";
import BranchTable from "@/views/system/group/BranchTable";

/**
 * 用户组织关系关联用户和组织
 */
export default {
  name: "UserTable",
  components: {BranchTable, UserInfo},
  mixins: [IdeaTable],
  props: {
    select: {
      type: Boolean,
      default: false
    },
    branchInfo: {
      type: Object,
      default: function () {
        return { branchId: '' }
      }
    }
  },
  watch: {
    'branchInfo.branchId'(){
      this.addFilter({ field: 'branchId', op: '=',value: this.branchInfo.branchId})
      this.entityInfo.branchId = this.branchInfo.branchId
      this.loadSource()
    },
    branchVisible(){
      if (!this.branchVisible) {
        this.selectUser = { adminFlag: false }
      }
    }
  },
  data(){
    return {
      branchVisible: false,
      selectUser: { adminFlag: false }
    }
  },
  created() {
    this.queryUrl = '/userGroup/list'
    this.saveUrl = '/user/save'
    this.removeUrl = '/userGroup/delete/'
    this.rowKey = 'userGroupId'
    this.fixedFilters.push({ field: 'branchId', op: '=',value: this.branchInfo.branchId})
    if (this.select) {
      this.loadSource()
    }
  },
  methods: {
    initEntity(){
      this.entityInfo = { dataState: 0, branchId: this.branchInfo.branchId }
    },
    addUser() {
      if (!this.branchInfo.branchId) {
        this.$resultMsg(this, { msg: '请选择组织'})
        return
      }
      this.visible = true
    },
    openBranch(row){
      this.selectUser = this.$Util.copy(row)
      this.selectUser.adminFlag = false
      this.branchVisible = true
    },
    switchBranch(row){
      this.selectUser.branchId = row.branchId
      const $this = this
      this.saveData(this.selectUser, false, function (res) {
        if (res.code === 200) {
          $this.branchVisible = false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
